/******************************************************************************
 **
 **    LISA Confidential
 **
 **    Source Materials
 **
 **    (C) COPYRIGHT LISA 2008-2009
 **
 **
 **  File Name:      GetTaskListForOneTaskCommand.java
 **  Description:
 **  Date:           2008-10-28
 **  Author:         LisaQian
 ******************************************************************************/
package com.ti.andon.workflow.command;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.jbpm.taskmgmt.exe.TaskInstance;

public class GetTaskListForOneTaskCmd extends org.jbpm.command.GetTaskListCommand
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6787259770883337034L;

	private static Log logger = LogFactory.getLog(GetTaskListForOneTaskCmd.class);
	
	/**
	 * The user whose tasks to be retrieved.
	 */
	Long actorId;

	/**
	 * The English node name of the task list.
	 */
	String taskName;

	/**
	 * Constructor.
	 * @param actor
	 * @param includeVariables
	 */
	public GetTaskListForOneTaskCmd(Long actorId, boolean includeVariables, String taskName)
	{
		super("", includeVariables);
		if (null != actorId)
		{
			this.setActor(actorId.toString());
		}
		this.actorId = actorId;
		this.taskName = taskName;
	}

	/**
	 * Retrieves the task list of the given actor and the task name.
	 * @param session hibernate session
	 * @return The list of the task instance of one specific node for one user
	 *  
	 * @throws Exception
	 */
	public Object execute(Session session) throws Exception
	{
		ArrayList specifiedTaskList = new ArrayList();
		if (null != actorId)
		{
			GetTaskListCmd getTaskListCommand = new GetTaskListCmd(this.actorId, true);
			Iterator iterTaskInstance = ((List) getTaskListCommand.execute(session)).iterator();
			TaskInstance taskInstance = null;
			while (iterTaskInstance.hasNext())
			{
				taskInstance = (TaskInstance) (iterTaskInstance.next());
				if (taskInstance.getTask().getName().equals(this.taskName) && taskInstance.isSignalling() == true)
				{
					specifiedTaskList.add(taskInstance);
				}
			}
		}
		else
		{
			logger.error("GetTaskListForOneTaskCmd.execute)() : actorId can not be null!!!");
		}
		return specifiedTaskList;
	}
}
